import * as Cesium from "cesium";
import { createApp, h } from "vue";
import NeiboPopup from "./duanmianPopup.vue";
import eventBus from "@/utils/eventBus";
let currentNeiboDialog = null;

export default class duanmianDialog {
  constructor(opts) {
    // 确保只存在一个实例
    if (currentNeiboDialog) {
      currentNeiboDialog.windowClose(true); // 立即关闭
    }

    const { viewer, ...rest } = opts;
    this.viewer = viewer;
    this.isDestroyed = false;
    
    // 存储断面相关的实体
    this.sectionEntities = {
      line: opts.sectionLine || null,
      startPoint: opts.startPoint || null,
      endPoint: opts.endPoint || null
    };
    
    this.createNeiboDialog({
      ...rest,
      closeEvent: this.windowClose.bind(this),
    });
    
    currentNeiboDialog = this;
  }

  createNeiboDialog(opts) {
    try {
      // 创建容器并添加到viewer的DOM中
      this.parentNode = document.createElement("div");
      this.parentNode.className = "neibo-profile-dialog-container";
      
      // 将弹窗添加到Cesium容器中
      this.viewer.cesiumWidget.container.appendChild(this.parentNode);
      
      this.app = createApp({
        render() {
          return h(NeiboPopup, {
            ...opts,
          });
        },
      });
      
      this.vmInstance = this.app.mount(this.parentNode);
      this.addPostRender();
    } catch (error) {
      console.error("创建弹窗失败:", error);
      this.windowClose();
    }
  }

  addPostRender() {
    if (this.viewer && !this.isDestroyed) {
      this.viewer.scene.postRender.addEventListener(this.postRender, this);
    }
  }

  postRender() {
    if (!this.vmInstance?.$el || !this.vmInstance.$el.style) return;
    
    // 固定弹窗位置
    this.vmInstance.$el.style.position = 'fixed';
    this.vmInstance.$el.style.top = '20%';
    this.vmInstance.$el.style.right = 'auto';
    this.vmInstance.$el.style.bottom = 'auto';
    this.vmInstance.$el.style.left = '16%';
    
    this.vmInstance.$el.style.display = "block";
  }

  windowClose(immediate = false) {
    if (this.isDestroyed) return;
    this.isDestroyed = true;

    const cleanup = () => {
      try {
        // 1. 移除事件监听
        if (this.viewer) {
          this.viewer.scene.postRender.removeEventListener(this.postRender, this);
        }
        
        // 2. 清除所有断面相关实体
        if (this.viewer) {
          Object.values(this.sectionEntities).forEach(entity => {
            if (entity) this.viewer.entities.remove(entity);
          });
        }
        eventBus.$emit("section-selection-cancelled");
        eventBus.$emit("section-dialog-closed");
        // 3. 卸载Vue应用
        if (this.app) {
          this.app.unmount();
          if (this.parentNode && this.parentNode.parentNode) {
            this.parentNode.parentNode.removeChild(this.parentNode);
          }
          this.app = null;
        }
        
        // 4. 清理引用
        if (currentNeiboDialog === this) {
          currentNeiboDialog = null;
        }
        
        this.vmInstance = null;
        this.parentNode = null;
        this.sectionEntities = {};
      } catch (e) {
        console.error("关闭弹窗时出错:", e);
      }
    };

    if (immediate) {
      cleanup();
    } else {
      // 延迟确保动画完成
      setTimeout(cleanup, 100);
    }
  }
}